package sweezy.swf.tools.abcbuilder.utils
{
	import flash.utils.Dictionary;
	import sweezy.swf.tools.abcbuilder.ElementDef;

	public class ElementList
	{

		private var _elements:Array;

		private var _numElements:uint;

		private var _uidMap:Dictionary;

		public function ElementList()
		{
			_elements = [];
			_numElements = 0;
			_uidMap = new Dictionary();
		}

		public function addElement(element:ElementDef):void
		{
			if (element === null)
			{
				return;
			}
			if (element.uid in _uidMap)
			{
				return;
			}

			_elements[_numElements++] = element;
			_uidMap[element.uid] = null;
		}

		public function get elements():Array
		{
			return _elements.slice();
		}

		public function getElementByUID(uid:uint):ElementDef
		{
			return _uidMap[uid];
		}

		public function get numElements():uint
		{
			return _numElements;
		}

		public function removeElement(element:ElementDef):void
		{
			if (element === null)
			{
				return;
			}

			for (var i:int = 0; i < _numElements; i++)
			{
				if (_elements[i] === element)
				{
					_elements.splice(i, 1);
					delete _uidMap[element.uid];
					_numElements--;
					return;
				}
			}
		}
	}
}
